home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk.zip / MEMORY.C < prev    next >
C/C++ Source or Header  |  1991-04-07  |  2KB  |  96 lines

  1.  
  2. /********************************************
  3. memory.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the Awk programming language as defined in
  8. Aho, Kernighan and Weinberger, The AWK Programming Language,
  9. Addison-Wesley, 1988.
  10.  
  11. See the accompaning file, LIMITATIONS, for restrictions
  12. regarding modification and redistribution of this
  13. program in source or binary form.
  14. ********************************************/
  15.  
  16.  
  17. /* $Log:    memory.c,v $
  18.  * Revision 2.1  91/04/08  08:23:35  brennan
  19.  * VERSION 0.97
  20.  * 
  21. */
  22.  
  23.  
  24. /* memory.c */
  25.  
  26. #include "mawk.h"
  27.  
  28. #ifdef  __TURBOC__
  29. #define SUPPRESS_NEW_STRING_PROTO  /* get compiler off our back on
  30.          the definition of new_STRING() */
  31. #pragma  warn -pro
  32. #endif
  33.  
  34. #include "memory.h"
  35.  
  36. STRING null_str = {1, 0, "" } ;
  37.  
  38. static STRING *char_string[127] ;
  39. /* slots for strings of one character
  40.    "\01" thru "\177"    */
  41.   
  42. STRING *new_STRING(s, xlen)   
  43.   char *s ;  unsigned xlen ;
  44.   /* WARNING: if s != NULL, don't access xlen
  45.      because it won't be there   */
  46. { register STRING *p ;
  47.   unsigned len ;
  48.  
  49.   if ( s )
  50.         switch( len = strlen(s) )
  51.         {
  52.             case 0 : 
  53.                 p = &null_str  ; p->ref_cnt++ ;
  54.                 break ;
  55.  
  56.             case 1 :
  57.                 if ( *(unsigned char *)s < 128 )
  58.                 {   if ( p = char_string[*s-1] )
  59.                         p->ref_cnt++ ;
  60.                     else
  61.                     { p = (STRING *) zmalloc(6) ;
  62.                       p->ref_cnt = 2 ;  p->len = 1 ; 
  63.                       p->str[0] = s[0] ;
  64.                       p->str[1] = 0 ;
  65.                       char_string[*s-1] = p ;
  66.                     }
  67.  
  68.                     break ; /*case */
  69.                 }
  70.                 /* else FALL THRU */
  71.  
  72.             default :
  73.                 p = (STRING *) zmalloc(len+5) ;
  74.                 p->ref_cnt = 1 ; p->len = len ;
  75.                 (void) memcpy( p->str , s, len+1) ;
  76.                 break ;
  77.         }
  78.   else  
  79.   { p = (STRING *) zmalloc( xlen+5 ) ;
  80.     p->ref_cnt = 1 ; p->len = xlen ;
  81.     /* zero out the end marker */
  82.     p->str[xlen] = 0 ; 
  83.   }
  84.  
  85.   return p ;
  86. }
  87.  
  88.  
  89. #ifdef   DEBUG
  90.  
  91. void  DB_free_STRING(sval)
  92.   register STRING *sval ;
  93. { if ( -- sval->ref_cnt == 0 )  zfree(sval, sval->len+5) ; }
  94.  
  95. #endif
  96.